const s = 'abcdefggi';

function method(s) {
  let start = 0, ans = -1;
  let seen = new Set();

  for (let end = 0; end < s.length; end++) {
    // 遇见了之前出现过的，需要清空set,从新位置开始
    while (seen.has(s[end])) {
      seen.delete(s[start]);
      start++;
    }

    // 清完之后
    seen.add(s[end]);
    ans = Math.max(ans, end - start + 1);
  }

  return ans;
}

console.log(method(s))